//
//  FlutterMapPOIPlugin.m
//  Runner
//
//  Created by mac on 2022/5/20.
//

#import "FlutterMapPOIPlugin.h"
#import "MapPOIViewController.h"

@interface FlutterMapPOIPlugin()<MapPOIViewDelgate>

@property (nonatomic, strong) UIViewController *controller;
@property (nonatomic, copy) FlutterResult result;

@end

//通道名称
static NSString *const CHANNEL_NAME = @"com.qingning.qn/ChoiceLocationUtils";
//通道方法
static NSString *const CHANNEL_METHOD = @"choice_location";
// 高德地图 key
static NSString *const GDMAP_KEY = @"76ffe13cd5b5e54de8a438aaec4fc8ca";

@implementation FlutterMapPOIPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:CHANNEL_NAME
            binaryMessenger:[registrar messenger]];


    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    FlutterMapPOIPlugin *plugin = [[FlutterMapPOIPlugin alloc]initWithViewController:viewController];

    [registrar addMethodCallDelegate:plugin channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([CHANNEL_METHOD isEqualToString:call.method]) {

      self.result = result;
      MapPOIViewController *mapVC = [[MapPOIViewController alloc] init];
      mapVC.address = call.arguments[@"address"];
      mapVC.delegate = self;
      UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mapVC];
      nav.modalPresentationStyle = 0;

      [self.controller presentViewController:nav animated:YES completion:nil];
  } else {
    result(FlutterMethodNotImplemented);
  }
}

#pragma make - 地图VC的代理
//将原生 数据 传给 flutter
- (void)mapPOIViewDelgate:(NSDictionary *)poiData {
    self.result(poiData);
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        self.controller = viewController;
    }
    return self;
}

@end
